package com.power.using.mybomb.adapter.base;

import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.power.using.mybomb.base.ImageLoaderFactory;

/**
 * Created by 聪明一只哈 on 2020/3/20.
 * Time: 2020/3/20  10:20
 */
public class BaseRecyclerHolder extends RecyclerView.ViewHolder {

    private final SparseArray<View> mViews;
    public int layoutId;

    public BaseRecyclerHolder(int layoutId,View itemView) {
        super(itemView);
        this.layoutId=layoutId;
        this.mViews=new SparseArray<>(8);
    }

    public SparseArray<View> getAllView(){
        return mViews;
    }

    /**
     *
     * @param viewId
     * @param <T>
     * @return
     */
    protected <T extends View> T getView(int viewId){
        View view=mViews.get(viewId);
        if(view==null){
            view=itemView.findViewById(viewId);
            mViews.put(viewId,view);
        }

        return (T)view;
    }

    /**
     *
     * @param viewId
     * @param text
     * @return
     */
    public BaseRecyclerHolder setText(int viewId,String text){
       TextView view=getView(viewId);
       view.setText(text);
       return this;
    }

    /**
     * 设置Enable
     * @param viewId
     * @param enable
     * @return
     */
    public BaseRecyclerHolder setEnable(int viewId,boolean enable){

        View v = getView(viewId);
        v.setEnabled(enable);
        return this;
    }


    /**
     * 点击事件
     * @param viewId
     * @param listener
     * @return
     */
    public BaseRecyclerHolder setonClickListener(int viewId,View.OnClickListener listener){
        View v=getView(viewId);
        v.setOnClickListener(listener);
        return this;
    }

    /**
     * 是否可见
     * @param viewId
     * @param visibility
     * @return
     */
    public BaseRecyclerHolder setVisible(int viewId,int visibility){
        View view=getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    /**
     *
     * @param viewId
     * @param drawabledId
     * @return
     */
    public BaseRecyclerHolder setImageResource(int viewId,int drawabledId){
        ImageView view=getView(viewId);
        view.setImageResource(drawabledId);
        return this;
    }


    /**
     *
     * @param viewId
     * @param bm
     * @return
     */
    public BaseRecyclerHolder setImageBitmap(int viewId, Bitmap bm){
        ImageView view=getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     *
     * @param avatar
     * @param defaultRes
     * @param viewId
     * @return
     */
    public BaseRecyclerHolder setImageView(String avatar,int defaultRes,int viewId){
        ImageView iv=getView(viewId);
        ImageLoaderFactory.getLoader().loadAvator(iv,avatar,defaultRes);
        return this;
    }








}
